package com.lark;

import com.lark.constant.AppConstant;
import com.lark.constant.FileConstant;
import com.lark.view.splash.LoadingScreen;
import javafx.application.Application;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.StandardOpenOption;

@Slf4j
public class Main {
    public static void main(String[] args) {
        File file = new File(FileConstant.DEFAULT_CONF_PATH, ".alarmClock/alarmClock.lock");
        File fileParent = file.getParentFile();
        if (fileParent != null && !fileParent.exists()) {
            //创建文件夹
            fileParent.mkdirs();
        }
        try (FileChannel fc = FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
             FileLock lock = fc.tryLock()) {
            if (lock == null) {
                log.warn("another instance is running");
                System.exit(AppConstant.ALREADY_RUNNING_EXIT_CODE);
            }
            Application.launch(TestApplication.class, args);
        } catch (IOException e) {
            log.error("File channel operation encountered an exception: ", e);
            System.exit(AppConstant.ALREADY_RUNNING_EXIT_CODE);
        }
    }
}
